//index.js
var t = getApp(),
  a = t.requirejs("core"),
  c = t.requirejs("api/index"),
  e = (t.requirejs("icons"), t.requirejs("wxParse/wxParse"));//对图片进行处理
const myAudio = wx.createInnerAudioContext();
Page({
  data: {
    route: "power",
    menu: t.tabBar,
    menu_show:false,
    indicatorDots: true,
    autoplay: true,
    interval: 5000,
    duration: 500,
    circular: true,
    icons: t.requirejs("icons"),
    total: 0,
    page: 1,
    show:false,
    display: true,
    hotimg: "/yb_mingpian/static/images/hotdot.jpg",
    notification: "/yb_mingpian/static/images/notification.png",
    default_pic: '/yb_mingpian/static/images/add_pic.jpg',
    background: t.config.background,
    showtabbar: false,
    markers: [{
      iconPath: "/yb_mingpian/static/images/red_position_icon.png",
      title:'地理位置',
      latitude: 34.6284500000,
      longitude: 112.4282100000,
      width: 50,
      height: 50
    }],
    video: false,
    config: t.config,
    id:"0",
    diy_id:"0",
    bookData:{},//清空表单数据用
    isPlaying: false,
    isPlay: false,
    audioSrc: '',
    nowtime: "00:00",
    duration: 0,
    currecttime: 0,
    timechanging: false,
    audioIndex: null
  },
  //底部导航跳转
  menu_url: function (k) {
    a.menu_url(k, 2);
  },
  //跳转+收集表单id
  formSubmit: function (k) {
    var e = {},
      vv = { currentTarget: { dataset: {} } },
      data = k.detail.value;
    vv.currentTarget.dataset = data;
    a.menu_url(vv, 1);
    e.formid = k.detail.formId;
    e.openid = getApp().getCache("userinfo").openid;
    e.username = getApp().getCache("userinfo").nickName;
    a.get('Market/getFormid', e, function (t) {
      console.log(t)
    })
  },
  //跳转
  to_url: function (k) {
    a.menu_url(k, 1);
    myAudio.destroy()
  },

  // 招聘跳转
  to_zhaopin:function(e){
    var id=a.pdata(e).id;
    if(id){
      a.jump('/yb_mingpian/pages/zhaopin/index?id=' + id);
    }
  },

  // //跳转
  // to_url: function (k) {
  //   var data = a.pdata(k),
  //     url = data.url ? data.url : '',
  //     appid = data.appid ? data.appid : '',
  //     path = data.path ? data.path : '',
  //     title = data.title ? data.title : '',
  //     path = data.path ? data.path : '',
  //     title = data.title ? data.title : '';
  //   c.to_url(data.key, url, appid, path, title);
  // },
  onLoad: function (options) {

    console.log(options);
    var that=this;
    if (options != null && options != undefined)
    {
      that.setData({ 
        diy_id: options.id,
        tabbar_index: options.tabbar_index ? options.tabbar_index : -1
      });
    }

      that.getList();
      a.setting();
  },
  formBook:function(e){
    var that=this,
    data=e.detail.value,
    uid = getApp().getCache("userinfo").uid,
    nickName = getApp().getCache("userinfo").nickName,
    avatarUrl = getApp().getCache("userinfo").avatarUrl;
    if (!nickName && !avatarUrl) {
      a.error('请先登录');
      return;
    }

    if (data.name==''){
      a.error('姓名不能为空');
      return;
    } else if (uid==null) {
      a.error('请先登录');
      setTimeout(function(){
        a.jump('/yb_mingpian/pages/index/index',2);
      },1000)	
      return;
    }else if(data.phone.length!=11){
      a.error('手机号格式不正确');
      return;
    }
    // else if (data.booktime == '') {
    //   a.error('预约时间不能为空');
    //   return;
    // }

    data.user_id = uid;
    a.get('Index/WriteBook',data,function(t){
      if(t.code==0){
        that.setData({
          bookData:{}
        });
        a.success('提交成功');
      }else{
        a.alert(t.msg);
      }
    })
   
  },

  onShow: function () {

  },
  getList:function(){
    var that = this,
    id=that.data.diy_id;
    console.log("洛枳学姐", that)
    console.log("洛枳学姐",id)
    c.power(id,that, function (t) {
      console.log(t,'iiiiiiiiiiii')
      that.setData(t);
    }, 'ModShop')
  },
  /**
 * 下拉刷新
 */
  onPullDownRefresh: function () {
    this.getList();
    wx.stopPullDownRefresh();
  },
  /**
   * 地图
   */
  navigate: function (e) {
    var t = a.pdata(e);
    if (t.name && t.lat && t.lng) {
      a.tx_map(t.lat, t.lng, t.name)
    } else {
      a.toast('不能获取到该位置')
    }
  },
  phone: function (t) {
    a.phone(t)
  },
  // 万能表单提交
  formPower: function (e) {
    c.formSubmit(this, e);
  },
  bindPickerChange: function (e) {
    c.bindPickerChange(this, e);
  },
  listen_time_two: function (e) {
    c.listen_time_two(this, e);
  },
  chooseImageTap1: function (e) {
    var id = a.pdata(e).id;
    c.upload(this, id, 'form_pic', 1)
  },
  chooseImageTap2: function (e) {
    var id = a.pdata(e).id;
    c.upload(this, id, 'form_pic', 0)
  },
  Image_del: function (e) {
    c.Image_del(this, e);
  },
  /**
   * 图片预览
   */
  previewImage:function(e){
   var url=a.pdata(e).url,
   arr=a.pdata(e).arr;
   a.previewImage(url, arr,'imgurl')
  },
  /**
  * 监听开始时间
  */
  listenerTime: function (e) {
    //调用setData()重新绘制
    this.setData({
      "bookData.book_time": e.detail.value,
    });
  },
  //分享
  // onShareAppMessage: function () {
  //   return a.onShareAppMessage();
  // },
  //音频组件播放
  play_status_change: function (e) {
    console.log(this, '515151')
    console.log(e, '515151')
    var that = this;
    myAudio.src = that.data.audioSrc;
    // var index = e.currentTarget.dataset['index'];
    // myAudio.src="";
    // that.setData({
    //   audioIndex : index
    // });
    // if (that.data.audioIndex == e.currentTarget.dataset['index'] ){
    //   console.log('22222222222222222222')
    //   myAudio.src = that.data.audioSrc;
    //   console.log(myAudio.src,'zzzzzzzzz')
    //   myAudio.play();

    // }
    // else{
    //   // that.setData({
    //   //   audioIndex:null
    //   // });
    //   // myAudio.pause();
    //   // myAudio.destroy()
    // }
    if (this.data.isPlay) {
      myAudio.pause();
      this.setData({
        isPlay: false,
      })
    }
    else {
      myAudio.play();
      this.setData({ isPlay: true });
    }
    myAudio.onCanplay(function (res) {
      console.log("onCanplay !!!!!!!!!");
      that.setData({
        duration: parseInt(myAudio.duration)
      })
    })
    myAudio.onPlay(function (res) {
      console.log("onPlay !!!!!!!!!");
      that.setData({
        duration: parseInt(myAudio.duration)
      })

      // that.startAnimationInterval();
    })
    myAudio.onEnded(function (res) {
      console.log("onEnded !!!!!!!!!");
      that.setData({
        currecttime: 0,
        isPlay: false,
        nowtime: "00:00"
      });
      myAudio.destroy()
    })
    myAudio.onPause(function (res) {
      console.log("onPause !!!!!!!!!");
      that.setData({
        isPlay: false,
      })
    })
    myAudio.onTimeUpdate(function (res) {
      console.log("onTimeUpdate !!!!!!!!!");
      if (!that.data.timechanging) {
        that.setData({
          currecttime: parseInt(myAudio.currentTime),
          nowtime: that.timeformat(parseInt(myAudio.currentTime))
        })
      }
    })
  },

  // 视频播放
  video: function (event) {
    var index = event.currentTarget.dataset['index'];

    if (!this.data.videoIndex) { // 没有播放时播放视频
      this.setData({
        videoIndex: index
      })
      var videoContext = wx.createVideoContext('video' + index)
      videoContext.play()
    } else {
      //停止正在播放的视频
      var videoContextPrev = wx.createVideoContext('video' + this.data.videoIndex)
      videoContextPrev.stop()
      //将点击视频进行播放
      this.setData({
        videoIndex: index
      })
      var videoContextCurrent = wx.createVideoContext('video' + index)
      videoContextCurrent.play()
    }
  },

  timeformat: function (time) {
    var m = parseInt(time / 60);
    m = m < 10 ? "0" + m : "" + m;
    var s = time % 60;
    s = s < 10 ? "0" + s : "" + s;
    return m + ":" + s;
  },
  timechanging: function (res) {
    this.setData({
      timechanging: true,
    })
  },
  timechange: function (res) {
    console.log("timechange !!!!!!!!");
    var time = res.detail.value;
    wx.createInnerAudioContext().seek(time);
    this.setData({
      currecttime: ti,
      me,
      timechanging: false,
    })
  },
  onUnload: function () {
    myAudio.destroy()
  },
  onHide: function () {
    myAudio.destroy()
  }
})